package com.jt.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.jt.pojo.Cart;
import com.jt.pojo.User;
import com.jt.service.DubboCartService;
import com.jt.util.UserThreadLocal;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller

public class CartController {
    private final static String JTUSER = "JT_USER";
    @Reference(check = false)
    private DubboCartService dubboCartService;
    @RequestMapping("/cart/show")
    public String show(Model model, HttpServletRequest request){
        //自定义userId
        Long userId= UserThreadLocal.get().getId();
        List<Cart> cartList = dubboCartService.findCartList(userId);
        model.addAttribute("cartList",cartList);
        return "cart";
    }
    @RequestMapping("/cart/update/num/{itemId}/{num}")
    @ResponseBody
    public void updateCartNum(Cart cart,HttpServletRequest request){
        //获取userId
        Long userId= UserThreadLocal.get().getId();
        //Long userId=7L;
        cart.setUserId(userId);
        dubboCartService.updateCartNum(cart);
    }
    @RequestMapping("/cart/delete/{itemId}")
    public String deleteCart(Cart cart){
        Long userId= UserThreadLocal.get().getId();
        cart.setUserId(userId);
        dubboCartService.deleteCart(cart);
        return "redirect:/cart/show.html";
    }
    @RequestMapping("/cart/add/{itemId}")
    public String saveCart(Cart cart){
        Long userId = UserThreadLocal.get().getId();
        cart.setUserId(userId);
        dubboCartService.saveCart(cart);
        return "redirect:/cart/show.html";
    }

}
